import { Matrix4, Vector2, Matrix3, Vector3, Vector4, Color } from "@nirvana/math";
import { WebGLCanvas } from "@nirvana/canvas";
import { Camera, ParticleSet } from "@nirvana/core";

import { ParticleSystem, ColourInterpolatorAffector, BoxEmitter} from "@nirvana/particles";

async function main() {
  // 使用使用 Canvas
  let canvas = document.getElementById("engine-nirvana") as HTMLCanvasElement;
  const webCanvas = new WebGLCanvas(canvas, {});
  const engine = webCanvas.engine;
  const scene = engine.sceneManager.createScene("root");

  const camera = new Camera("root camera", engine);
  scene.addCamera(camera);
  camera.transform.setPosition(0, 0, 10);

  const gl = engine.gl;

  const psys = new ParticleSystem("smoke-1");
  psys.aspectratio = camera.aspect;
  psys.setParticleQuota(1000);
  psys.setEmittedEmitterQuota(6);
  psys.defaultWidth = 0.1;
  psys.defaultHeight = 0.1;
  psys.isEmitting = true;
  // psys.material = "/dev-static/textures/clover.jpg";
  // psys.material = "/dev-static/textures/ball.png";
  // psys.material = "/dev-static/textures/b-light.png";
  psys.material = "/dev-static/textures/taohua.png";
  psys.stacks = 2;
  psys.slices = 2;

  // const boxEmitter = psys.addEmitter("Box");
  // boxEmitter.name = "fragment";
  // // boxEmitter.emittedEmitter = "explosionnew1";
  // boxEmitter.position.cloneFrom(new Vector3(-1.1, -1.0, 0));
  // boxEmitter.enabled = true;
  // boxEmitter.emitted = true;
  // boxEmitter.setTimeToLive(0.17, 15);
  // boxEmitter.setRepeatDelay(5.5, 8.5);
  // boxEmitter.setSize(3, 0, 1);
  // boxEmitter.emissionRate = 1;
  // boxEmitter.xRangeFactor = 0.1;
  // boxEmitter.yRangeFactor = 0.0;
  // boxEmitter.zRangeFactor = 0.1;
  // boxEmitter.setDirection(new Vector3(0, 1, 0));
  // boxEmitter.setMinTexcoordIndex(0);
  // boxEmitter.setMaxTexcoordIndex(2);
  // boxEmitter.setAngle(Math.PI / 4);
  // // boxEmitter.setParticleVelocity(4000, 4000);
  // // boxEmitter.setDuration(0.1, 0.1);

  // const boxEmitter2 = psys.addEmitter("Box");
  // boxEmitter2.name = "fragment2";
  // boxEmitter2.position.x = 0.5;
  // boxEmitter2.enabled = true;
  // boxEmitter2.emitted = true;
  // boxEmitter2.setTimeToLive(0.17, 0.23);
  // boxEmitter2.setRepeatDelay(3.5, 3.5);
  // boxEmitter2.setSize(100, 100, 100);
  // boxEmitter2.emissionRate = 12;
  // boxEmitter2.xRangeFactor = 0.1;
  // boxEmitter2.yRangeFactor = 0.1;
  // boxEmitter2.zRangeFactor = 0.1;

  const pointEmitter = psys.addEmitter("Box") as BoxEmitter;
  pointEmitter.position.set(-1, 1.1, 0);
  pointEmitter.name = "pointEmitter";
  pointEmitter.emissionRate = 2;
  // pointEmitter.setColourRangeStart(new Color(1, 1, 1, 1));
  // pointEmitter.setColourRangeEnd(new Color(1, 1, 1, 1));
  pointEmitter.setSize(4, 0, 0);
  pointEmitter.setAngle(Math.PI / 2);
  pointEmitter.setMinTexcoordIndex(0);
  pointEmitter.setMaxTexcoordIndex(3);
  // pointEmitter.setRepeatDelay(0.1, 0.1);
  pointEmitter.setTimeToLive(0.9, 6);
  pointEmitter.setDirection(new Vector3(0, -1, 0));
  pointEmitter.setParticleVelocity(0.1, 0.5);

  // const pointEmitter2 = psys.addEmitter("Point");
  // pointEmitter2.position.set(0.6, 0, 0);
  // pointEmitter2.name = "explosionnew1";
  // pointEmitter2.emissionRate = 100;
  // pointEmitter2.setColourRangeStart(new Color(1, 1, 1, 1));
  // pointEmitter2.setColourRangeEnd(new Color(1, 1, 1, 1));
  // pointEmitter2.setAngle(Math.PI / 2);
  // pointEmitter2.setMinTexcoordIndex(0);
  // pointEmitter2.setMaxTexcoordIndex(2);
  // pointEmitter2.setRepeatDelay(0.1, 0.5);
  // pointEmitter2.setTimeToLive(0.3, 0.9);
  // // pointEmitter2.setDirection(new Vector3(0, 1, 0));
  // pointEmitter2.setParticleVelocity(0.1, 0.4);

  // const ringEmitter = psys.addEmitter("Ring");
  // ringEmitter.name = "Ellipsoid";
  // ringEmitter.emissionRate = 100;
  // ringEmitter.setTimeToLive(9, 10);
  // // ringEmitter.xRangeFactor = 0.1;
  // // ringEmitter.yRangeFactor = 0.1;
  // // ringEmitter.zRangeFactor = 0.1;
  // ringEmitter.setSize(2, 0.5, 1);
  // ringEmitter.setDirection(new Vector3(0, 0, 1));

  // const linearForceAffector = psys.addAffector("LinearForce");
  // linearForceAffector.mForceVector.y = 5;
  // // linearForceAffector.mForceVector.x = 0;
  // linearForceAffector.mForceApplication = "FA_ADD";

  const rotationAffector = psys.addAffector("Rotation");
  rotationAffector.mRotationSpeedRangeStart = -Math.PI / 2.0;
  rotationAffector.mRotationSpeedRangeEnd = Math.PI / 2;
  rotationAffector.mRotationRangeStart = -Math.PI;
  rotationAffector.mRotationRangeEnd = Math.PI;

  const colourInterpolator = psys.addAffector("ColourInterpolator") as ColourInterpolatorAffector;
  colourInterpolator.setTimeAdjust(0, 0);
  colourInterpolator.setTimeAdjust(1, 0.2);
  colourInterpolator.setTimeAdjust(2, 1);
  colourInterpolator.setColourAdjust(0, new Color(1, 1, 1, 0));
  colourInterpolator.setColourAdjust(1, new Color(1, 1, 1, 1));
  colourInterpolator.setColourAdjust(2, new Color(1, 1, 1, 0));

  // const deflectorPlane = psys.addAffector("DeflectorPlane");
  // deflectorPlane.mPlanePoint.set(0, 0, 0);
  // deflectorPlane.mPlaneNormal.set(0, 1, 0);
  // deflectorPlane.mBounce = 1.0;

  // psys.addToManager();
  const particleMesh = new ParticleSet(psys);
  scene.addEntity(particleMesh);

  // const ps = new ParticleSet(psys);
  // scene.addEntity(ps);

  engine.update = () => {
    // console.log("update");
    // psys.transform.
  };
}

main();
